﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using MiniScrum.Models;

namespace MiniScrum
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            filters.Add(new RequireAuthenticationAttribute());

        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = "" } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            /*
             Configuration data
             * Once we need to deploy to production then we create a script to setup thi information. 
             */

            using (MiniScrumDB db = new MiniScrumDB())
            {
  
                Role adminRole = db.Roles.Where(r => r.RoleName.Equals("Admin")).SingleOrDefault();
                Role memberRole = db.Roles.Where(r => r.RoleName.Equals("TeamMember")).SingleOrDefault();

                if (adminRole == null)
                {
                    adminRole = new Role() { RoleName = "Admin" };
                    db.Roles.Add(adminRole);
                }
                User user = adminRole.Users.Where(u => u.UserName == "admin").FirstOrDefault();
                if (user == null)
                {
                    user = new User() { Email = "admin@miniscrum.com", FullName = "admin", Password = "admin", UserName = "admin" };
                    adminRole.Users.Add(user);
                }
                if (memberRole == null)
                    db.Roles.Add(new Role { RoleName = "TeamMember" });

                db.SaveChanges();

            }
        }

        protected void Application_OnAuthenticateRequest(Object sender, EventArgs e)
        {
            if (Context.User != null)
            {
                if (Context.User.Identity.IsAuthenticated)
                {
                    MiniScrum.Models.User user = MiniScrum.Models.User.GetUserByName(Context.User.Identity.Name);

                    if (user == null)
                    {
                        throw new ApplicationException("Context.User.Identity.Name is not a recognised user.");
                    }

                    System.Threading.Thread.CurrentPrincipal = Context.User = user;
                    return;
                }
            }
            System.Threading.Thread.CurrentPrincipal = Context.User = null;
        }

    }
}